package com.qf.day4.huihua;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @date 2022/3/28
 * @desc
 */
@WebServlet("/huihua1")
public class HuiHua1Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");


        String productId = req.getParameter("productId");

        if(productId.equals("1")){
            String message = "你购买的是apple笔记本";

            //往request 域对象存放数据
             req.setAttribute("message",message);

            //this.getServletContext().setAttribute("message",message);

            Cookie cookie = new Cookie("product", message);

            cookie.setMaxAge(1000);

            resp.addCookie(cookie);


            req.getRequestDispatcher("/day4/huihua/shopCar.jsp").forward(req,resp);
        }else{
            String message = "你购买的是联想笔记本";
            //req.setAttribute("message",message);

            //this.getServletContext().setAttribute("message",message);

            Cookie cookie = new Cookie("product", message);

            //cookie.setPath("/");   // 访问这个路径 携带 cookie

            cookie.setMaxAge(1000);

            resp.addCookie(cookie);

            req.getRequestDispatcher("/day4/huihua/shopCar.jsp").forward(req,resp);

        }


    }
}
